package com.hjq.controller;

import com.hjq.common.PageResult;
import com.hjq.common.Result;
import com.hjq.entity.vo.CollectionCourseVO;
import com.hjq.entity.vo.PageParam;
import com.hjq.service.CollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 收藏管理
 */
@Slf4j
@RestController
@RequestMapping("/collect")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;
    /**
     * 分页查询收藏课程
     * @return PageResult<CollectionCourseVO>
     */
    @GetMapping
    public Result<PageResult<CollectionCourseVO>> list( PageParam pageParam) {
        log.info("分页查询收藏课程");
        PageResult<CollectionCourseVO> result = collectionService.findByUserIdAndPage(pageParam);
        return  Result.success(result);
    }
    /**
     * 添加收藏课程
     * @param courseId 课程ID
     */
    @PostMapping
    public Result<Object> add(Long courseId) {
        log.info("添加收藏课程,{}",courseId);
        collectionService.add(courseId);
        return Result.success("添加成功");
    }
    /**
     * 删除收藏课程
     * @param id 用户收藏表ID
     */
    @DeleteMapping("{id}")
    public Result<Object> delete(Long id) {
        log.info("删除收藏课程,{}",id);
        Integer count= collectionService.delete(id);
        return count==1?Result.success("删除成功"):Result.error("删除失败");
    }

}
